package com.yanqu.road.dao.impl.channelcenter.luckydraw;

import com.yanqu.road.dao.cache.SqlCacheParam;
import com.yanqu.road.dao.impl.TempNoPoolCacheDao;
import com.yanqu.road.entity.activity.luckydraw.LuckyDrawRewardConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class TempLuckyDrawRewardConfigDaoImpl extends TempNoPoolCacheDao {

    public TempLuckyDrawRewardConfigDaoImpl(String dbParam) {
        super(dbParam);
    }

    public List<LuckyDrawRewardConfig> getLuckyDrawRewardConfigList(int activityId) {

        String sql = "select * from `t_n_activity_washepvp_draw` where `activityId`=?";
        SqlCacheParam sqlCacheParam = getCacheParam(sql);
        sqlCacheParam.addParam(Integer.class, activityId);
        // 先读缓存
        Object cacheResult = getCacheResult(sqlCacheParam);
        if (cacheResult != null) {
            return (List<LuckyDrawRewardConfig>) cacheResult;
        }

        List<LuckyDrawRewardConfig> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = createCall(sqlCacheParam);
            rs = call.executeQuery();
            while (rs.next()) {
                LuckyDrawRewardConfig luckyDrawRewardConfig = new LuckyDrawRewardConfig();
                luckyDrawRewardConfig.setActivityId(rs.getInt("activityId"));
                luckyDrawRewardConfig.setShootId(rs.getInt("shootId"));
                luckyDrawRewardConfig.setReward(rs.getString("reward"));
                luckyDrawRewardConfig.setNum(rs.getInt("num"));
                luckyDrawRewardConfig.setWeight(rs.getInt("weight"));
                luckyDrawRewardConfig.setPlace(rs.getInt("place"));
                list.add(luckyDrawRewardConfig);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection(call, rs);
        }
        // 添加缓存
        addCacheResult(sqlCacheParam, list);
        return list;
    }

}
